package eureka.ui.auction;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import eureka.domain.account.WebsiteUser;
import eureka.domain.auction.Auction;
import eureka.domain.auction.AuctionState;
import eureka.exceptions.DatabaseException;
import eureka.service.auction.IAuctionService;
import eureka.service.auction.impl.AuctionService;
import util.lang.RefValue;
import util.web.page.PageBean;

/**
 * Servlet implementation class ShowAuctionsByState
 */
@WebServlet("/Auctions/ShowAuctionsByState")
public class ShowAuctionsByState extends HttpServlet {
	private static final long serialVersionUID = 1L;
	  private IAuctionService as = new AuctionService();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShowAuctionsByState() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		WebsiteUser wuser = (WebsiteUser) session.getAttribute("user");

		if (wuser == null) {
			response.sendRedirect(request.getAttribute("contextPath")
					+ "/Account/Login?redirect_url="
					+ URLEncoder.encode(
							request.getAttribute("contextPath")
									+ "/Auctions/ShowAuctionsByState?"
									+ request.getQueryString(), "UTF-8"));
			return;
		}

		String currentpagestr = request.getParameter("curPage");
		String pagesizestr = request.getParameter("pageSize");

		long currentpage = 0;
		long pagesize = 0;

		try {
			currentpage = Long.parseLong(currentpagestr);
			if (currentpage <= 0)
				currentpage = 1;
		} catch (Exception e) {
			currentpage = 1;
		}
		try {
			pagesize = Long.parseLong(pagesizestr);
			if (pagesize <= 0 || pagesize > 100) {
				pagesize = 20;
			}
		} catch (Exception e) {
			pagesize = 20;
		}

		RefValue<Long> count = new RefValue<Long>();
		PageBean<Auction> bipage = new PageBean<Auction>();
		Short state = Short.parseShort(request.getParameter("state"));
		bipage.setCurrentPage(currentpage);
		bipage.setPageSize(pagesize);

		List<Auction> list = null;

		try {
			list = as.getAuctions(0, 0, wuser.getUID(), AuctionState.fromCode(state), 0, 0, 0);
			count.setValue(as.getAuctionsCount(0, 0, wuser.getUID(), AuctionState.fromCode(state), 0));
		} catch (DatabaseException e) {
			response.sendError(500);
			return ;
		}
		bipage.setRecords(list, count.getValue());

		String param = "listauction" + AuctionState.getStateStr(state) + ".jsp";
		request.setAttribute("page", bipage);
		request.getRequestDispatcher(param).forward(request, response);
	}

}
